#include <bits/stdc++.h>
using namespace std;
#define int long long

void solve() {
    int n; cin >> n;
    vector<int> a(n);
    for(auto& x : a) cin >> x;
    vector<vector<int>> sum(n + 1, vector<int>(32));
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < 32; j++) {
            sum[i][j] = sum[i - 1][j] + (a[i - 1] >> j & 1);
        }
    }
    int q; cin >> q;
    while(q--) {
        int x, k;
        cin >> x >> k;
        x--;
        if(a[x] >= k) {
            cout << -1 << ' ';
            continue;
        }
        int l = 0, r = n - x - 1;
        auto check = [&](int t) {
            int res = 0;
            for(int i = 0; i < 32; i++) {
                if(sum[x + t + 1][i] - sum[x][i] == t + 1) {
                    res += 1 << i;
                }
            }
            return res >= k;
        };
        while(l < r) {
            int mid = (l + r + 1) / 2;
            if(check(mid)) l = mid;
            else r = mid - 1;
        }
        cout << x + l + 1 << ' ';
    }
    cout << '\n';
}

signed main() {
    int t;
    cin >> t; while(t--) solve();
    return 0;
}